package com.wn55.hrms.web.controller;

import com.github.pagehelper.PageInfo;
import com.wn55.hrms.entity.Dimission;
import com.wn55.hrms.service.DimissionService;
import com.wn55.hrms.util.ResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author henry
 */

@RestController
@RequestMapping("dimission")
@Api(value = "此接口用于操作离职管理功能")
public class DimissionController {

    DimissionService dimissionService;

    @Autowired
    public void setDimissionService(DimissionService dimissionService){this.dimissionService = dimissionService;}

    /**
     * 分页查询所有离职数据
     * @return
     */
    @GetMapping("/loadMenu")
    @ApiOperation(value = "分页查询离职数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",dataType = "int",name="now",value = "当前页",defaultValue = "1"),
            @ApiImplicitParam(paramType = "query",dataType = "int",name="size",value = "每页条数",defaultValue = "2"),
            @ApiImplicitParam(paramType = "query",dataType = "string",name="empName",value = "员工姓名模糊查询")
    })
    public ResultResponse<PageInfo<Dimission>> initDimissionList(
            @RequestParam(value = "now",defaultValue = "1") Integer now ,
            @RequestParam(value = "size",defaultValue = "2")Integer size,
            @RequestParam(value = "name",required = false)String name){
        ResultResponse<PageInfo<Dimission>> result;
        try {
            PageInfo<Dimission> pageInfo = dimissionService.initDimissionMenu(now,size,name);
            result = new ResultResponse<>(200,"查询离职数据成功!",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询离职数据失败!");
        }
        return result;
    }

    /**
     * 添加离职信息
     * @param dimission
     * @return
     */
    @PostMapping("/addDimission")
    @ApiOperation("新增离职")
    public ResultResponse<Void> addDimission(@RequestBody Dimission dimission){
        ResultResponse<Void> result = null;
        try {
            dimission.setIsDelete(0);
            dimission.setLeaveTime(new Date());
            dimission.setEffectiveTime(new Date());
            dimissionService.add(dimission);
            result = new ResultResponse<>(200,"新增离职信息成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"新增离职信息失败!");
        }
        return result;
    }

    @PutMapping("/modifyDimission")
    @ApiOperation("修改离职信息")
    public ResultResponse<Void> modifyContract(@RequestBody Dimission dimission){
        ResultResponse<Void> result = null;
        try {
            dimission.setIsDelete(0);
            dimission.setEffectiveTime(new Date());
            dimissionService.update(dimission);
            result = new ResultResponse<>(200,"修改离职信息成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改离职信息失败!");
        }

        return result;
    }


    @DeleteMapping("deleteDimssion/{id}")
    @ApiOperation("根据员工工号删除离职信息")
    @ApiImplicitParam(paramType = "path",dataType = "String",name="id",value = "员工工号")
    public ResultResponse<Void> deleteContract(@PathVariable(value = "id") String id){
        ResultResponse<Void> result = null;
        try {
            dimissionService.delete(id);
            result = new ResultResponse<>(200,"删除离职信息成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除离职信息失败!");
        }
        return result;
    }

}
